
var apiHost = wx.getStorageSync('apiHost'); //请求外网服务器链接
var yunId = wx.getStorageSync('yunId'); //yunId
var openId = wx.getStorageSync('openId'); //openId
Page({
  cateGoryList: [] ,//初始化分类lists
  data:{
    showModal: false,       //提示框是否显示
    showQueModal: false,    //确认框
    cateGoryId : '',        //分类id
    isAdd : true,            //是否添加
    msg:'该分类中包含商品，您确认要删除吗？'
  },
  //页面加载
  onLoad:function(options){
    var that = this;
    that.init(); //初始化方法
  },
  //初始化方法
  init:function(e){
    var that = this;
      wx.request({
        url: apiHost +'xcx/category/list',
        data:{
          yunId: yunId
        },
        success:function(res){
          var code = res.data.code
          var msg = res.data.msg
          if (code=="10000"){
            var cateGorys = res.data.data;
            that.setData({
              cateGoryList: cateGorys
            });
          }else{
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 2000,
              mask: true
            });
          }
        }
      })
  },
  //添加分类方法
  addCategoryModel : function(){
    var that = this;

    that.setData({
      showModal: true,
      isAdd : true,
      cateGoryId : '',        //分类id
      onclickCon: "onConfirm"
    });
  },
  //新建分类 确认 表单提交
  addCateGoryBox: function (e){
    var that = this;
    var cateName = e.detail.value.name; //分类名称
    var cateGoryId = that.data.cateGoryId;              //分类id

      if(cateGoryId==''){
          that.addOrDetailCategory(cateName,'');
      }else{
          that.addOrDetailCategory(cateName,cateGoryId);
      }
  },
    /**
     * 添加分类方法
     * @param cateName   分类名称
     */
  addOrDetailCategory: function (cateName,cateGoryId){

    var that = this;

    wx.request({
      url: apiHost + 'xcx/category/addOrEdit',
      data : {
        yunId : yunId,
        openId : openId,
        name: cateName,
        categoryId : cateGoryId
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.onCancel();//关闭对话框
        that.init(); //初始化方法
      },
    })
  },
    /**
     * 编辑分类
     * @param e
     */
  updateCategory : function(e){
    var that = this;
    var nameId = e.target.dataset.toggle;
    var nameIdlist = nameId.split(",");
    var name  = nameIdlist[0];
    var cateGoryId  = nameIdlist[1];

      that.setData({
          isAdd : false,
          showModal: true,
          name : name,
          cateGoryId : cateGoryId
      });
  },
    /**
     * 删除分类
     * @param e
     */
  delCategory : function(e){
    var that = this;
      var cateGoryId = e.target.dataset.toggle;

        that.setData({
            showQueModal: true,
            cateGoryId : cateGoryId
        });
  },
  onDel : function(e){
      var that = this;
      var cateGoryId = that.data.cateGoryId;

      wx.request({
          url: apiHost + 'xcx/category/del',
          data : {
              yunId : yunId,
              openId : openId,
              categoryId : cateGoryId
          },
          header: {
              'content-type': 'application/json' // 默认值
          },
          success: function (res) {
              that.hideQueModal();//关闭对话框
              that.init(); //初始化方法
          },
      })
  },
  /**
     * 隐藏模态对话框
     */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  hideQueModal: function () {
    this.setData({
      showQueModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    this.hideModal();
  },
  onQueCancel: function () {
    this.hideQueModal();
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.hideModal();
  },

})